﻿Public Class Worker
    Public Shared objMyLock As New Object
    Public Shared NumberOfThreads As Integer
    Private traceTable As String = My.Settings.sqlTable
    Dim mdx As New MdxHelper

    Public Sub mdxWorker()
        Dim QueryID As Integer = 0
        Dim con As New SqlClient.SqlConnection(My.Settings.sqlConnectionString)
        Dim TextData As String = ""
        Dim RequestParameters As String = ""
        Dim tickCount As Integer = 0
        Dim queryCount As Integer
        Dim RunID As Integer = replayController.RunID
        Dim CalculateHash As Boolean = My.Settings.CalculateHash
        Dim Hash As String = ""
        Dim cellCount As Integer = 0



        SyncLock objMyLock
            NumberOfThreads += 1
        End SyncLock

        mdx.Connect()

        Dim Success As Boolean = True
        While Success And replayController.QueryID <= My.Settings.MaxQueryCount - 1
            QueryID = replayController.IncrementQueryID()
            Hash = ""
            cellCount = 0

            Try
                queryCount += 1
                tickCount = Environment.TickCount

                Call getNextQuery(con, QueryID, TextData, RequestParameters)

                If My.Settings.CalculateHash Then
                    mdx.ExecuteMDXwithChecksum(TextData, RequestParameters, Hash, cellCount)
                Else
                    Call mdx.ExecuteMDX(TextData, RequestParameters)
                End If

                tickCount = Environment.TickCount - tickCount
                If My.Settings.LogResults Then
                    If Len(Hash) = 0 Then Hash = ""
                    If cellCount = Nothing Then cellCount = 0
                    SqlHelper.ExcecuteNonQuery("usp_runlog_query", New SqlClient.SqlParameter("RunID", RunID) _
                                               , New SqlClient.SqlParameter("QueryID", QueryID) _
                                               , New SqlClient.SqlParameter("Duration", tickCount / 1000) _
                                               , New SqlClient.SqlParameter("Hash", Hash) _
                                               , New SqlClient.SqlParameter("CellCount", cellCount))
                End If

                replayController.IncrementCompletedQueryCount()
            Catch ex As Exception
                tickCount = Environment.TickCount - tickCount
                replayController.IncrementError()

                My.Application.Log.DefaultFileLogWriter.WriteLine(String.Format("Error in Query {0}", QueryID))
                If Not IsNothing(MdxHelper.errLastMdxQuery) Then My.Application.Log.DefaultFileLogWriter.Write(MdxHelper.errLastMdxQuery.ToString)
                If Not IsNothing(MdxHelper.errLastParameters) Then My.Application.Log.DefaultFileLogWriter.Write(MdxHelper.errLastParameters)
                My.Application.Log.DefaultFileLogWriter.Write(ex.ToString)
                My.Application.Log.DefaultFileLogWriter.Flush()

                If My.Settings.LogResults Then
                    SqlHelper.ExcecuteNonQuery("usp_runlog_query", New SqlClient.SqlParameter("RunID", RunID) _
                                               , New SqlClient.SqlParameter("QueryID", QueryID) _
                                               , New SqlClient.SqlParameter("Duration", tickCount / 1000) _
                                                , New SqlClient.SqlParameter("ErrorMessage", ex.ToString) _
                                               , New SqlClient.SqlParameter("Hash", Hash) _
                                               , New SqlClient.SqlParameter("CellCount", cellCount))
                End If

            End Try
        End While

        SyncLock objMyLock
            NumberOfThreads -= 1
        End SyncLock
        con.Close()
        con.Dispose()
        mdx.DisConnect()

    End Sub

   

    Public Sub getNextQuery(con As SqlClient.SqlConnection, QueryID As Integer, ByRef TextData As String, ByRef RequestParameters As String)
        Dim sql As String = String.Format("Select TextData, RequestParameters FROM [{0}] WHERE QueryID={1}", traceTable, QueryID)

        Using dt As DataTable = SqlHelper.ExcecuteDataSet(con, sql, Nothing).Tables(0)
            If dt.Rows.Count = 0 Then
                TextData = ""
                RequestParameters = ""
            Else
                TextData = dt.Rows(0)(0).ToString
                RequestParameters = dt.Rows(0)(1).ToString
            End If
        End Using
    End Sub
End Class
